package tagdb.ui;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import tagdb.ui.filter.DenovoSearchFilter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * LoadDialog is used for finding specific entries in an JXTable
 *
 * @author Thilo Muth
 */
public class FilterSpectraDialog extends JDialog {

    /**
     * Parent main frame.
     */
    private MainFrame parent;

    /**
     * Peptide sequence textfield.
     */
    private JTextField pepSeqTtf;

    /**
     * Tag sequence textfield.
     */
    private JTextField tagSeqTtf;

    /**
     * Score textfield.
     */
    private JTextField scoreTtf;

    /**
     * Spectrum title textfield.
     */
    private JTextField spectrumTitleTtf;

    /**
     * OK button.
     */
    private JButton okBtn;

    /**
     * Cancel button.
     */
    private JButton cancelBtn;

    /**
     * Peptide sequence checkbox.
     */
    private JCheckBox pepSeqCbx;

    /**
     * Tag sequence checkbox.
     */
    private JCheckBox tagSeqCbx;

    /**
     * Score checkbox.
     */
    private JCheckBox scoreCbx;

    /**
     * Spectrum title checkbox.
     */
    private JCheckBox spectrumTitleCbx;

    /**
     * The current denovo search filter (if any available).
     */
    private DenovoSearchFilter currentFilter;

    /**
     * Constructor for the filter spectra dialog.
     * @param parent The parent frame.
     * @param modal Flag for dialog being modal.
     */
    public FilterSpectraDialog(MainFrame parent, boolean modal) {
        super(parent, modal);
        this.parent = parent;
        initComponents();
    }

    /**
     * Constructor for the filter spectra dialog.
     * @param parent The parent frame.
     * @param modal Flag for dialog being modal.
     */
    public FilterSpectraDialog(MainFrame parent, boolean modal, DenovoSearchFilter currentFilter) {
        super(parent, modal);
        this.parent = parent;
        this.currentFilter = currentFilter;
        initComponents();
    }

    private void initComponents() {
        setTitle("Filter Spectra By...");
        CellConstraints cc = new CellConstraints();
        Container cp = getContentPane();
        JPanel searchPnl = new JPanel(new FormLayout("5dlu, p, 5dlu, p, 5dlu", "5dlu, p, 5dlu, p, 5dlu, p, 5dlu, p, 5dlu"));

        pepSeqTtf = new JTextField(15);
        pepSeqTtf.setMaximumSize(new Dimension(pepSeqTtf.getMaximumSize().width, pepSeqTtf.getPreferredSize().height));

        tagSeqTtf = new JTextField(15);
        tagSeqTtf.setMaximumSize(new Dimension(tagSeqTtf.getMaximumSize().width, tagSeqTtf.getPreferredSize().height));

        scoreTtf = new JTextField(15);
        scoreTtf.setMaximumSize(new Dimension(scoreTtf.getMaximumSize().width, scoreTtf.getPreferredSize().height));
        spectrumTitleTtf = new JTextField(15);
        spectrumTitleTtf.setMaximumSize(new Dimension(spectrumTitleTtf.getMaximumSize().width, spectrumTitleTtf.getPreferredSize().height));

        pepSeqCbx = new JCheckBox("Peptide Sequence: ", false);
        pepSeqCbx.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(pepSeqCbx.isSelected()){
                    tagSeqCbx.setSelected(false);
                    scoreCbx.setSelected(false);
                    spectrumTitleCbx.setSelected(false);
                    pepSeqTtf.setEnabled(true);
                    tagSeqTtf.setEnabled(false);
                    scoreTtf.setEnabled(false);
                    spectrumTitleTtf.setEnabled(false);
                } else {
                    pepSeqTtf.setEnabled(false);
                }
            }
        });

        tagSeqCbx = new JCheckBox("Tag Sequence: ", false);
        tagSeqCbx.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(tagSeqCbx.isSelected()){
                    pepSeqCbx.setSelected(false);
                    scoreCbx.setSelected(false);
                    spectrumTitleCbx.setSelected(false);
                    tagSeqTtf.setEnabled(true);
                    pepSeqTtf.setEnabled(false);
                    scoreTtf.setEnabled(false);
                    spectrumTitleTtf.setEnabled(false);
                } else {
                    tagSeqTtf.setEnabled(false);
                }
            }
        });

        scoreCbx = new JCheckBox("Score >= ", false);
        scoreCbx.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(scoreCbx.isSelected()){
                    pepSeqCbx.setSelected(false);
                    tagSeqCbx.setSelected(false);
                    spectrumTitleCbx.setSelected(false);
                    scoreTtf.setEnabled(true);
                    pepSeqTtf.setEnabled(false);
                    tagSeqTtf.setEnabled(false);
                    spectrumTitleTtf.setEnabled(false);
                } else {
                    scoreTtf.setEnabled(false);
                }
            }
        });

        spectrumTitleCbx = new JCheckBox("Spectrum Title: ", false);
        spectrumTitleCbx.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(spectrumTitleCbx.isSelected()){
                    pepSeqCbx.setSelected(false);
                    tagSeqCbx.setSelected(false);
                    scoreCbx.setSelected(false);
                    pepSeqTtf.setEnabled(false);
                    tagSeqTtf.setEnabled(false);
                    spectrumTitleTtf.setEnabled(true);
                    scoreTtf.setEnabled(false);
                } else {
                    spectrumTitleTtf.setEnabled(false);
                }
            }
        });

        searchPnl.add(pepSeqCbx, cc.xy(2, 2));
        searchPnl.add(tagSeqCbx, cc.xy(2, 4));
        searchPnl.add(scoreCbx, cc.xy(2, 6));
        searchPnl.add(spectrumTitleCbx, cc.xy(2, 8));

        searchPnl.add(pepSeqTtf, cc.xy(4, 2));
        searchPnl.add(tagSeqTtf, cc.xy(4, 4));
        searchPnl.add(scoreTtf, cc.xy(4, 6));
        searchPnl.add(spectrumTitleTtf, cc.xy(4, 8));

        // Bottom + button panel
        JPanel buttonPnl = new JPanel(new FormLayout("5dlu, p, 5dlu, p, 5dlu", "p"));
        JPanel bottomPnl = new JPanel(new BorderLayout());

        // OK button
        okBtn = new JButton("Filter");
        okBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DenovoSearchFilter filter = new DenovoSearchFilter();
                filter.setPeptideSequence(pepSeqTtf.getText());
                parent.setDenovoSearchFilter(filter);
                dispose();
            }
        });

        // Cancel button - Dispose the dialog.
        cancelBtn = new JButton("Cancel");
        cancelBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        buttonPnl.add(okBtn, cc.xy(2, 1));
        buttonPnl.add(cancelBtn, cc.xy(4, 1));
        bottomPnl.add(buttonPnl, BorderLayout.EAST);

        cp.setLayout(new BorderLayout());
        cp.add(searchPnl, BorderLayout.CENTER);
        cp.add(bottomPnl, BorderLayout.SOUTH);

        pack();
        setLocationRelativeTo(getOwner());
        setResizable(false);
        setVisible(true);
    }
}

